package clientView;

import clientService.ClientConnectServerThread;
import clientService.FileClientService;
import clientService.MessageClientService;
import clientService.UserClientService;
import common.User;
import utils.Utility;

/**
 * @author lhn
 * @date 2024-04-30 12:22
 * 客户端的菜单界面
 */

public class QQView {
    private boolean loop = true;    //控制是否显示菜单
    private String key = "";    //接收用户的键盘输入
    private UserClientService userClientService = new UserClientService();
    private MessageClientService messageClientService = new MessageClientService();
    private FileClientService fileClientService = new FileClientService();

    //显示主菜单
    private void mainMenu() {
        while (loop) {
            System.out.println("=====欢迎登录网络通讯系统=====");
            System.out.println("\t\t 1 登录系统");
            System.out.println("\t\t 9 退出系统");
            System.out.print("请输入你的选择");

            key = Utility.readString(1);
            switch (key){
                case "1":
                    System.out.print("请输入账号:");
                    String userId = Utility.readString(50);
                    System.out.print("请输入密码:");
                    String password = Utility.readString(50);
//                    User user = new User(userId,password);
//                    System.out.println(user);
                    //发送到服务器进行验证
                    //TODO
                    boolean flag = userClientService.login(userId, password);
                    if (flag) {
                        System.out.println("登陆成功");
                        System.out.println("=====欢迎(用户"+userId+"登录成功)=====");
                        //二级菜单
                        while (loop) {
                            System.out.println("\n=====网络通信系统二级菜单(用户"+userId+")=====");
                            System.out.println("\t\t 1 显示用户在线列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 9 退出系统");
                            System.out.print("请输入你的选择:");

                            key = Utility.readString(1);
                            switch (key){
                                case "1":
                                    System.out.println("显示用户在线列表");
                                    //获取在线用户列表
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.println("群发消息");
                                    System.out.print("请输入想对大家说的话:");
                                    String content1 = Utility.readString(100);
                                    //发送消息给所有人
                                    messageClientService.sendMessageToAll(content1, userId);
                                    break;
                                case "3":
                                    System.out.println("私聊消息");
                                    System.out.println("请输入想要聊天的用户id:");
                                    String getterId = Utility.readString(50);
                                    System.out.print("请输入消息内容:");
                                    String content = Utility.readString(100);
                                    //发送消息
                                    messageClientService.sendMessageToOne(content, userId, getterId);
                                    break;
                                case "4":
                                    System.out.println("发送文件");
                                    System.out.println("请输入你想把文件发送给的用户(在线用户):");
                                    getterId = Utility.readString(50);
                                    System.out.print("请输入发送文件的路径(形式d:\\\\xx.jpg):");
                                    String src = Utility.readString(100);

                                    System.out.print("请输入把文件发送到对应的路径(形式d:\\yy.jpg):");
                                    String dest = Utility.readString(100);
                                    fileClientService.sendFileToOne(src, dest, userId, getterId);
                                    //TODO 接收者自己选择接收路径(目前无法实现)
//                                    fileClientService.sendFileToOneNotDest(src, userId, getterId);
                                    break;
                                case "9":
                                    System.out.println("退出系统");
                                    //调用方法，向服务器发送退出的message
                                    userClientService.logout();
                                    loop = false;
                                    break;
                        }
                    }
                    }else {
                        System.out.println("=====登录失败=====");
                    }
                    break;
                case "9":
                    System.out.println("退出系统");
                    loop = false;
                    break;
            }

        }
    }

    public static void main(String[] args) {
        new QQView().mainMenu();
        System.out.println("客户端退出系统...");
    }
}
